using UnityEngine;
using UnityEditor;
using System.Collections;

//[AddComponentMenu("Bone/TestBone")]
public class TestBone : MonoBehaviour {

	public GameObject ChangeParts1;
	public GameObject ChangeParts2;
	//private System.Collections.Generic.List<Transform> orignalBone;
	Transform []orignalBone;
	Mesh orignalMesh;
	Material orignalMaterial;
	Matrix4x4 []orignalMat;
	
	/*
	 * [MenuItem("Bone/BoneList")]
	static void BoneList()
	{
		
	}
	*/
	/*
	override void OnInspectorGUI()
	{
		base.OnInspectorGUI();
		
		Debug.Log(target);
	}*/

	// Use this for initialization
	void Start () {
		
/*		SkinnedMeshRenderer []thisRenderer = this.GetComponents<SkinnedMeshRenderer>();
		
		Debug.Log("################ Start Bone List" + ToString() );
		
		foreach ( SkinnedMeshRenderer smr in thisRenderer )
		{
			
			Debug.Log( "	SMR Name :" + smr.ToString() );
			
			for( int i = 0 ; i < smr.bones.Length ; i++ )
			{
				
				Debug.Log( "		Bone Name :" + smr.bones[i].name );
			}
			
		}
*/
		if ( ChangeParts1 )
		{
			
			SkinnedMeshRenderer changeRenderer = ChangeParts1.GetComponent<SkinnedMeshRenderer>();
			/*
			//foreach ( SkinnedMeshRenderer currentRenderer in thisRenderer )
			{
				
				//SkinnedMeshRenderer currentRenderer = thisRenderer[0];
				Debug.Log( "Current Name :" + changeRenderer.name );
				
				string[] MyBones = new string[changeRenderer.bones.Length];
				
				for (int i = 0; i < changeRenderer.bones.Length; i++)
				{
					
					if ( changeRenderer.bones[i] )
						
						Debug.Log ("BoneName:" + changeRenderer.bones[i].name);
					
				}				
				
			}*/
			
		}
	}
	
	public void OnDrawGizmos()
	{
		{
			
			Gizmos.color = Color.red;
			
			//Gizmos.DrawCube(transform.position, new Vector3(0.2f,0.2f,0.2f));
			//Gizmos.DrawLine(transform.position,transform.position+Vector3.up);
			
			SkinnedMeshRenderer []thisRenderer = this.GetComponents<SkinnedMeshRenderer>();
			
			foreach ( SkinnedMeshRenderer currentRenderer in thisRenderer )
			{

				for ( int i = 0 ; i < currentRenderer.bones.Length ; i++ )
				{
					
					if ( currentRenderer.bones[i] )
					{
						
						Gizmos.DrawSphere( currentRenderer.bones[i].transform.position, 0.002f);
						
					}
					
				}
			}
			
		}
	}
	
	// Update is called once per frame
	void Update () {

		if ( Input.GetKeyUp(KeyCode.A )  )
		{
			
			cSkinnedBase obase = (cSkinnedBase)this.GetComponentInChildren<cSkinnedBase>();
			if ( obase )
			{
				obase.SetDefaultSkinned();
				
				//GameObject newParts = (GameObject)Instantiate(ChangeParts2);
				//newParts.gameObject.transform.parent = transform;
				//cSkinnedBase sBase = ChangeParts2.GetComponent<cSkinnedBase>();
				//sBase.GetSkinnedInfo();
				//obase.AttachSkinned( sBase );
				SkinnedMeshRenderer []tarSMR = ChangeParts1.GetComponents<SkinnedMeshRenderer>();
				obase.AttachSkinned(tarSMR);
			}
			
			//GameObject chgObject = (GameObject)Instantiate(ChangeParts);
			
/*			SkinnedMeshRenderer changeRenderer = ChangeParts1.GetComponents<SkinnedMeshRenderer>()[0];
			
			SkinnedMeshRenderer []thisRenderer = this.GetComponents<SkinnedMeshRenderer>();
			
			var existBones = new System.Collections.Generic.List<Transform>();
			
			SkinnedMeshRenderer currentRenderer = thisRenderer[0];
				
			foreach ( SkinnedMeshRenderer smr in thisRenderer )
			{
				
				if ( smr.name == changeRenderer.name )
				{
					
					currentRenderer = smr;
					
				}
				
			}
			
			if ( currentRenderer == null )
				
				return;

			if ( orignalBone != null )
			{
				currentRenderer.bones = orignalBone;
				currentRenderer.sharedMesh = orignalMesh;
				currentRenderer.sharedMaterial = orignalMaterial;
				currentRenderer.sharedMesh.bindposes = orignalMat;
			}
			
			for (int j = 0; j < changeRenderer.bones.Length; j++ )
			{
				
				if ( changeRenderer.bones[j] )
				{
					
					for (int i = 0; i < currentRenderer.bones.Length; i++)
					{
						
						if ( currentRenderer.bones[i] )
						{
							
							if ( changeRenderer.bones[j].name == currentRenderer.bones[i].name )
							{
								
								existBones.Add(currentRenderer.bones[i].transform);
								break;
								
							}
	
						}
						
					}
				
				}
					
			}

			if ( orignalBone == null )
			{
				orignalBone = currentRenderer.bones;
				orignalMesh = currentRenderer.sharedMesh;
				orignalMaterial = currentRenderer.sharedMaterial;
				orignalMat = currentRenderer.sharedMesh.bindposes;
			}

			currentRenderer.sharedMesh = changeRenderer.sharedMesh;
			currentRenderer.sharedMesh.boneWeights = changeRenderer.sharedMesh.boneWeights;
			currentRenderer.sharedMesh.bindposes = changeRenderer.sharedMesh.bindposes;
			
			currentRenderer.bones = existBones.ToArray();
			
			currentRenderer.sharedMaterial = changeRenderer.sharedMaterial;
			*/
		}
		
		if ( Input.GetKeyUp(KeyCode.B )  )
		{
			
			cSkinnedBase obase = (cSkinnedBase)this.GetComponentInChildren<cSkinnedBase>();
			if ( obase )
			{
				obase.SetDefaultSkinned();
				
				//GameObject newParts = (GameObject)Instantiate(ChangeParts2);
				//newParts.gameObject.transform.parent = transform;
				//cSkinnedBase sBase = ChangeParts2.GetComponent<cSkinnedBase>();
				//sBase.GetSkinnedInfo();
				//obase.AttachSkinned( sBase );
				SkinnedMeshRenderer []tarSMR = ChangeParts2.GetComponents<SkinnedMeshRenderer>();
				obase.AttachSkinned(tarSMR);
			}
//			if ( sBase )
			//	sBase.SetDefaultSkinned();
			//Destroy( newParts );
			/*
			//GameObject chgObject = (GameObject)Instantiate(ChangeParts);
			
			SkinnedMeshRenderer changeRenderer = ChangeParts2.GetComponents<SkinnedMeshRenderer>()[0];
			
			SkinnedMeshRenderer []thisRenderer = this.GetComponents<SkinnedMeshRenderer>();
			
			var existBones = new System.Collections.Generic.List<Transform>();
			
			SkinnedMeshRenderer currentRenderer = thisRenderer[0];
				
			foreach ( SkinnedMeshRenderer smr in thisRenderer )
			{
				
				if ( smr.name == changeRenderer.name )
				{
					
					currentRenderer = smr;
					break;
					
				}
				
			}
			
			if ( currentRenderer == null )
				
				return;

			if ( orignalBone != null )
			{
				currentRenderer.bones = orignalBone;
				currentRenderer.sharedMesh = orignalMesh;
				currentRenderer.sharedMaterial = orignalMaterial;
				currentRenderer.sharedMesh.bindposes = orignalMat;
			}
			
			for (int j = 0; j < changeRenderer.bones.Length; j++ )
			{
				
				if ( changeRenderer.bones[j] )
				{
					
					for (int i = 0; i < currentRenderer.bones.Length; i++)
					{
						
						if ( currentRenderer.bones[i] )
						{
							
							if ( changeRenderer.bones[j].name == currentRenderer.bones[i].name )
							{
								
								existBones.Add(currentRenderer.bones[i].transform);
								break;
								
							}
	
						}
						
					}
				
				}
					
			}

			if ( orignalBone == null )
			{
				orignalBone = currentRenderer.bones;
				orignalMesh = currentRenderer.sharedMesh;
				orignalMaterial = currentRenderer.sharedMaterial;
				orignalMat = currentRenderer.sharedMesh.bindposes;
			}

			currentRenderer.sharedMesh = changeRenderer.sharedMesh;
			currentRenderer.sharedMesh.boneWeights = changeRenderer.sharedMesh.boneWeights;
			currentRenderer.sharedMesh.bindposes = changeRenderer.sharedMesh.bindposes;
			
			currentRenderer.bones = existBones.ToArray();
			
			currentRenderer.sharedMaterial = changeRenderer.sharedMaterial;
			*/
		}
		
		if ( Input.GetKeyUp(KeyCode.O) )
		{
			cSkinnedBase sbase = GetComponent<cSkinnedBase>();
			if ( sbase )
			{
				sbase.SetDefaultSkinned();
			}
			/*
			SkinnedMeshRenderer []thisRenderer = this.GetComponents<SkinnedMeshRenderer>();
			
			var existBones = new System.Collections.Generic.List<Transform>();
			
			foreach ( SkinnedMeshRenderer currentRenderer in thisRenderer )
			{
			
				if ( orignalBone != null )
				{
					currentRenderer.bones = orignalBone;
					currentRenderer.sharedMesh = orignalMesh;
					currentRenderer.sharedMaterial = orignalMaterial;
					currentRenderer.sharedMesh.bindposes = orignalMat;
				}
			}
			*/
		}
		
	}
}
	
	
